package com.zlh123.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlh123.commom.util.PageResult;
import com.zlh123.entity.Book;
import com.zlh123.mapper.BookMapper;
import com.zlh123.service.IBookService;
import com.zlh123.vo.BookVo;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlh
 * @since 2025-01-13
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    @Override
    public PageResult<Book> findBooksByPage(int page, int pageSize) {
        //开始分页
        Page<Book> page1=this.page(new Page<>(page, pageSize));
        //分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    // 3. 条件带分页查询
    @Override
    public PageResult<Book> searchBook(Integer page, Integer pageSize, BookVo vo) {
        // 1. 进行分页带条件查询
        // 1.1 封装查询条件
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getBname()), "bname",  vo.getBname() )
                .like(StrUtil.isNotBlank(vo.getWriter()), "writer", vo.getWriter());
        // 1.2 开始分页查询
        Page<Book> page1 = this.page(new Page<>(page, pageSize), wrapper);
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    //添加book
    @Override
    public boolean addBook(Book book) {
        return this.save(book);
    }
}
